package com.spring.boot.entity.server;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.validation.BindingResult;

/**
 * @author 刘骄阳
 * @version 创建时间：2021-06-21
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult {

    /**
     * 操作成功
     */
    private static final int SUCCESS = 200;
    /**
     * 操作失败
     */
    private static final int FAILED = 500;
    /**
     * 参数校验失败
     */
    private static final int VALIDATE_FAILED = 201;
    /**
     * 未认证
     */
    private static final int UNAUTHORIZED = 401;
    /**
     * 未授权
     */
    private static final int FORBIDDEN = 403;

    private Integer code;
    private String message;
    private Object data;

    /**
     * 普通成功返回
     *
     * @param data 获取的数据
     */
    public CommonResult success(Object data) {
        this.code = SUCCESS;
        this.message = "操作成功";
        this.data = data;
        return this;
    }

    /**
     * 普通成功返回
     */
    public CommonResult success() {
        this.code = SUCCESS;
        this.message = "操作成功";
        return this;
    }

    /**
     * 普通成功返回
     */
    public CommonResult success(String msg, Object data) {
        this.code = SUCCESS;
        this.message = msg;
        this.data = data;
        return this;
    }

    /**
     * 普通成功返回
     */
    public CommonResult fail(int code) {
        this.code = code;
        if (code == 100) {
            this.message = "请先登录";
        }
        return this;
    }

    /**
     * 普通失败提示信息
     */
    public CommonResult failed() {
        this.code = FAILED;
        this.message = "操作失败";
        return this;
    }

    public CommonResult failed(String message, Object data) {
        this.code = FAILED;
        this.message = message;
        this.data = data;
        return this;
    }

    public CommonResult failed(String message) {
        this.code = FAILED;
        this.message = message;
        return this;
    }


    public CommonResult failed(Integer code, String message) {
        this.code = code;
        this.message = message;
        return this;
    }

    public CommonResult failed(Integer code, String message, Object data) {
        this.code = code;
        this.message = message;
        this.data = data;
        return this;
    }


    /**
     * 参数验证失败使用
     *
     * @param message 错误信息
     */
    public CommonResult validateFailed(String message) {
        this.code = VALIDATE_FAILED;
        this.message = message;
        return this;
    }

    /**
     * 未登录时使用
     *
     * @param message 错误信息
     */
    public CommonResult unauthorized(String message) {
        this.code = UNAUTHORIZED;
        this.message = "暂未登录或token已经过期";
        return this;
    }

    /**
     * 未授权时使用
     *
     * @param message 错误信息
     */
    public CommonResult forbidden(String message) {
        this.code = FORBIDDEN;
        this.message = "没有相关权限";
        return this;
    }

    /**
     * 参数验证失败使用
     *
     * @param result 错误信息
     */
    public CommonResult validateFailed(BindingResult result) {
        validateFailed(result.getFieldError().getDefaultMessage());
        return this;
    }

    /**
     * 普通失败提示信息
     */
    public CommonResult paramFailed() {
        this.code = FAILED;
        this.message = "参数失败";
        return this;
    }

    public CommonResult paramFailed(String message) {
        this.code = FAILED;
        this.message = message;
        return this;
    }
}
